package com.lanou.renrenxin.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;

import java.util.Collections;
import java.util.List;

/**
 * Created by dllo on 16/3/8.
 */
public class BlackListActivity extends BaseActivity {
    private ListView listView;
    private BlacklistAdapater adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacklist);

        listView = (ListView) findViewById(R.id.lv_blacklist);

        //从本地获取黑名单
        List<String> blacklist = EMContactManager.getInstance().getBlackListUsernames();
        // 显示黑名单列表
        if (blacklist != null) {
            Collections.sort(blacklist);
            adapter = new BlacklistAdapater(this, 1, blacklist);
            listView.setAdapter(adapter);
        }
        //注册上下文菜单
        registerForContextMenu(listView );
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.remove_from_blacklist, menu);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.remove_from_blacklist) {
            final String tobeRemoveUser = adapter.getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position);
            // 把目标user移出黑名单
            removeOutBlacklist(tobeRemoveUser);
            return true;
        }
        return super.onContextItemSelected(item);
    }
    /**
     * 移出黑名单
     *
     * @param tobeRemoveUser
     */
    void removeOutBlacklist(final String tobeRemoveUser) {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage("正在移除");
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        new Thread(new Runnable() {
            public void run() {
                try {
                    // 移出黑民单
                    EMContactManager.getInstance().deleteUserFromBlackList(tobeRemoveUser);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            pd.dismiss();
                            adapter.remove(tobeRemoveUser);
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            pd.dismiss();
                            Toast.makeText(BlackListActivity.this, "从黑名单中移除失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }

    /**
     * adapter
     */
    private class BlacklistAdapater extends ArrayAdapter<String> {

        public BlacklistAdapater(Context context, int textViewResourceId, List<String> objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_blacklist, null);
            }
            String username = getItem(position);
            TextView name = (TextView) convertView.findViewById(R.id.name);
            ImageView avatar = (ImageView) convertView.findViewById(R.id.EIV_avatar);

//            EaseUserUtils.setUserAvatar(getContext(), username, avatar);
//            EaseUserUtils.setUserNick(username, name);

            return convertView;
        }

    }

    /**
     * 返回
     *
     * @param view
     */
    public void back(View view) {
        finish();
    }
}
